package zzhyc.keystore

import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.bytedance.sdk.openadsdk.AdSlot
import com.bytedance.sdk.openadsdk.CSJAdError
import com.bytedance.sdk.openadsdk.CSJSplashAd
import com.bytedance.sdk.openadsdk.TTAdNative
import com.bytedance.sdk.openadsdk.TTAdSdk
import com.bytedance.sdk.openadsdk.mediation.MediationConstant
import com.bytedance.sdk.openadsdk.mediation.ad.MediationAdSlot
import com.bytedance.sdk.openadsdk.mediation.ad.MediationSplashRequestInfo
import zzhyc.keystore.databinding.ActivityWelcomeBinding

private const val TAG = "WelcomeActivity"

class WelcomeActivity : AppCompatActivity() {

    private val binding by lazy { ActivityWelcomeBinding.inflate(layoutInflater) }

    //是否强制跳转到主页面
    private var mForceGoMain: Boolean = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        loadSplashAd()
    }

    private fun buildSplashAdslot(): AdSlot {
        return AdSlot.Builder().setCodeId(ConstantsKey.CSJ_SPLASH_ID)  //广告位id
            .setImageAcceptedSize(
                UIUtils.getScreenWidthInPx(this), UIUtils.getScreenHeightInPx(this)
            )
            .setMediationAdSlot(
                MediationAdSlot.Builder()
                    .setMediationSplashRequestInfo(object : MediationSplashRequestInfo(
                        MediationConstant.ADN_PANGLE,
                        ConstantsKey.CSJ_DOUDI_ID,    //兜底代码位id
                        ConstantsKey.CSJ_APP_ID,  //AppId
                        ConstantsKey.CSJ_APP_KEY
                    ) {})
                    .build()
            )
            .build()
    }

    // 加载开屏广告
    private fun loadSplashAd() {
        val isReady = TTAdSdk.isSdkReady()
        Log.i(TAG, "isReady-- $isReady")
        val adNativeLoader = TTAdSdk.getAdManager().createAdNative(this)
        adNativeLoader.loadSplashAd(buildSplashAdslot(), object : TTAdNative.CSJSplashAdListener {
            override fun onSplashLoadSuccess(p0: CSJSplashAd?) {
                Log.i(TAG, "onSplashLoadSuccess")
            }

            override fun onSplashLoadFail(error: CSJAdError?) {
                //广告加载失败
                Log.i(TAG, "广告加载失败onSplashLoadFail-- code--${error?.code}，msg--${error?.msg}")
                goToMainActivity()
            }

            override fun onSplashRenderSuccess(csjSplashAd: CSJSplashAd?) {
                //广告渲染成功，在此展示广告
                showSplashAd(csjSplashAd) //注 ：splashContainer为展示Banner广告的容器
            }

            override fun onSplashRenderFail(p0: CSJSplashAd?, p1: CSJAdError?) {
                //广告渲染失败
                Log.i(TAG, "广告渲染失败 onSplashRenderFail-- code--${p1?.code}，msg--${p1?.msg}")
                goToMainActivity()
            }
        }, 3500)
    }

    //展示开屏广告
    fun showSplashAd(ad: CSJSplashAd?) {
        ad?.let {
            it.setSplashAdListener(object : CSJSplashAd.SplashAdListener {
                override fun onSplashAdShow(csjSplashAd: CSJSplashAd?) {
                    //广告展示
                    //获取展示广告相关信息，需要再show回调之后进行获取
                    val manager = it.mediationManager;
                    if (manager != null && manager.showEcpm != null) {
                        val ecpm = manager.showEcpm.ecpm //展示广告的价格
                        val sdkName = manager.showEcpm.sdkName  //展示广告的adn名称
                        val slotId = manager.showEcpm.slotId //展示广告的代码位ID
                        Log.i(
                            TAG,
                            "onSplashAdShow-- 价格-- ${ecpm}，名称-- ${sdkName}，代码位Id-- ${slotId}"
                        )
                    }
                }

                override fun onSplashAdClick(csjSplashAd: CSJSplashAd?) {
                    //广告点击
                }

                override fun onSplashAdClose(csjSplashAd: CSJSplashAd?, p1: Int) {
                    //广告关闭
                    goToMainActivity()
                }
            })
            binding.welcomeContent.removeAllViews()
            binding.welcomeContent.addView(ad.splashView)
            Log.i(TAG, "开屏广告展示出来")
//            it.showSplashView(binding.welcomeContent) //展示开屏广告
        }
    }

    private fun goToMainActivity() {
        val intent = Intent(this, TwoActivity::class.java)
        startActivity(intent)
        binding.welcomeContent.removeAllViews()
        finish()
    }

    override fun onResume() {
        //判断是否该跳转到主页面
        if (mForceGoMain) {
            goToMainActivity()
        }
        super.onResume()
    }

    override fun onStop() {
        super.onStop()
        mForceGoMain = true
    }

    override fun onDestroy() {
        super.onDestroy()
    }
}